
<template>
    <el-date-picker
    ref="myDate"
    v-if="showType=='element'"
     :style="styleNew"
     v-model="currentValue"
     :disabled="disabled"
     :id="id"
     :type="type"
     :editable="editable"
     :class="classs"
     :value-format="format"
      :picker-options="pickerOptions"
      @blur="blurs"
     placeholder="选择日期">
   </el-date-picker>
    <DatePicker v-else :type="type" :style="styleNew"
    ref="myDate"
     placeholder="选择日期"
     :id="id"
     :format="format"
     :readonly="disabled"
     :class="classs"
     :transfer="true"
     :value="valueDate"
     :options="pickerOptions"
      @on-change="changeDate"
      @on-open-change="openChange"
    ></DatePicker>
</template>

<script>
   export default {
       props:['id','styles','value','data','index','isBorder','dataField','isSearch','extype','exkey','isreadonly','isDatetime','fontSize'],
       name: "input-date",
       data(){
           return {
               style2:"",
               type:"year",
               format:"yyyy",
               currentValue:this.value,
               classs:'',
               pId:"",
               cId:"",
               styleNew:"width:100%",
               pickerOptions: {},
               oldValue:"",
               disabled:false,
               editable:false,
               valueDate:"",
               isreadonlys:"N",
               showType:sessionStorage.getItem("isApp")=="Y" ? "element":"design",
               styleNew:"width:100%;"
           }
       },
       mounted(){
           let sef=this;
           this.pickerOptions={
               disabledDate(time) {
                   if(sef.extype=="start"){
                           return time.getTime() > Date.now() - 8.64e6;  //如果没有后面的-8.64e6就是不可以选择今天的
                   }else if(sef.extype=="start2"){//主要给结算日期用，限制只能提前多少天
                       let day=Number(sef.exkey.day);
                       return time.getTime()+1000*60*60*24*day < Date.now() +8.64e6;  //如果没有后面的-8.64e6就是不可以选择今天的
                   }else if(sef.extype&&sef.extype.indexOf("end")!=-1){
                       if(sef.exkey){
                           let vals=sef.extype.split("#");
                           let hour=24;
                           if(vals.length>1){
                               hour=Number(vals[1]);
                           }
                           let isNowDay="Y";
                           if(vals.length>2){
                               isNowDay=vals[2];
                           }
                           var myDate = new Date();
                           let hours=myDate.getHours();
                           let startOpr=(time.getTime() < Date.now() - 8.64e7);
                           
                           if(hours<hour){
                               if(isNowDay=="N"){
                                   startOpr=(time.getTime() < Date.now());
                                  
                               }
                           }else{
                               startOpr=(time.getTime() < Date.now()+1000*60*60*24 )
                           }
                          
                           //return (time.getTime() < Date.now() - 8.64e7)||(time.getTime() > Date.now() +(Number(sef.exkey)*1000*60*60*24))
                           return startOpr||(time.getTime() > Date.now() +(Number(sef.exkey)*1000*60*60*24))
                       }else{
                           return time.getTime() < Date.now() - 8.64e7;  //如果没有后面的-8.64e7就是不可以选择今天的 
                       }
                   }
                   
               }
           }
           try{
               this.$refs.myDate.$parent.$parent.$children[0].$el.style.fontSize=this.fontSize
           }catch(e){}
       },
       created(){
           if(this.fontSize){
               let fontSize="font-size:"+this.fontSize+";";
               this.styleNew=fontSize+this.styleNew;
           }
          
           if(localStorage.getItem("sCode")=='kmxbyg'){
               this.showType="element"
           }
           if(this.isDatetime=='Y'){
               this.type="datetime"
               this.format="yyyy-MM-dd HH:mm"
           }
           if(this.isSearch=="Y"){
               this.styleNew=this.styleNew+this.styles;
           }
           this.setData();
           try{
               if(this.exkey.readonlys){
                   let isReadonly_="Y"
                   for(let i=0;i<this.exkey.readonlys.length;i++){
                       console.log("readonlys=="+this.exkey.readonlys[i])
                       let ids=this.exkey.readonlys[i].id.split(".");
                       
                       let opr=this.exkey.readonlys[i].opr;
                       let vals=this.exkey.readonlys[i].val;
                       let val_="";
                       if(ids[0]=="本身"){
                           val_=this.currentValue;
                       }else{
                           if(ids.length==2)
                               val_=this.data[ids[0]][ids[1]];
                           else
                               val_=this.data[this.pId][ids[0]]
                       }
                       if(val_==undefined||val_=="undefined")
                           val_="";
                       //console.log("vals==="+vals+"=="+val_+"=="+opr+"=="+ids[0]+"=="+this.currentValue)
                       if(opr=="等于"&&vals!=val_){
                           //this.isreadonlys="Y";
                           isReadonly_="N"
                       }
                       if(opr=="不等于"&&vals==val_){
                           //this.isreadonlys="Y";
                           isReadonly_="N"
                       }

                   }
                   this.isreadonlys=isReadonly_;
               }
           }catch(e){}

           console.log("inputDate===="+this.id+"=="+this.isreadonlys)
           console.log(this.exkey)
           if(this.isreadonly=="Y"||this.isreadonlys=="Y"){
               this.disabled=true;
           }
           if(localStorage.getItem("isApp")!="Y"){
               this.editable=true;
           }
           
       },
       methods:{
           blurs(){

           },
           setData(){
              try{
                  if(this.isBorder=='N')
                   this.classs="no-border";
                   if(this.isSearch!="Y"){
                           if(!this.value){
                               let ids=this.id.split(".");
                               if(ids.length>1){
                                   let p=ids[0];
                                   let id_=ids[1];
                                   this.pId=p;
                                   this.cId=id_;
                                   if(this.index||this.index=='0'){
                                       try{
                                           this.currentValue=unescape(this.data[p][this.index][id_]);
                                       }catch(e){}
                                   }else{
                                       try{
                                           this.currentValue=unescape(this.data[p][id_]);
                                       }catch(e){}
                                   }
                               }else{
                                   
                                   if(this.dataField){
                                       if(this.data[this.dataField][this.index][this.id])
                                           this.currentValue=unescape(this.data[this.dataField][this.index][this.id]);
                                   }else{
                                       if(this.index!=""&&(this.index==0||this.index>0)){
                                           this.currentValue= unescape(this.data[this.index][this.id]);
                                       }else if(this.data[this.id])
                                           this.currentValue= unescape(this.data[this.id]);
                                   }
                               }
                               if(!this.style){
                                   this.style="width:180px"
                               }
                           }else{
                               
                               let data={};
                               data.id=this.id;
                               data.val=this.value;
                               data.index=this.index;
                               this.$emit('input',JSON.stringify(data));
                           }
                   }else{
                       this.currentValue=unescape(this.data[this.index].vals)
                   }
                   this.oldValue=this.currentValue;
              }catch(e){
                  
              }
              this.valueDate=this.currentValue;
              console.log("this.curDate==============="+this.currentValue)
           },
          changeDate(date){
             
              /*let year=date.getFullYear();
              let month=date.getMonth()+1;
              let day=date.getDate();
              if(month<10){
                  month="0"+month;
              }
              if(day<10){
                  day="0"+day;
              }
              let days=year+"-"+month+"-"+day;*/

              this.currentValue=date;
          },
          openChange(value){
             if(value){
                 let elems=document.getElementsByClassName("ivu-select-dropdown");
                 for(let i=0;i<elems.length;i++){
                     elems[i].style.zIndex="9999 !important"
                 }
                 
             }
          }
       },
       computed:{
           dataFields:function(){
               let ids=this.id.split(".");
               if(ids.length>1){
                   this.pId=ids[0];
                   this.cId=ids[1]
               }
               if(this.pId)
                   return this.data[this.pId][this.cId];
               else if(this.dataField){
                   return this.data[this.dataField][this.index];
               }else{
                   return this.data[this.index]
               }
           }
       },
       watch:{
           currentValue: function (val) { //currentValue值变动就向父组件传值
               if(this.oldValue==val){
                   return;
               }
              this.oldValue=val;
               this.GLOBAL.isChange=true;
               if(!val){
                   val="";
               }
               if(this.isSearch!="Y"){
                   if(this.dataField){
                       let data_=this.data[this.dataField][this.index];
                       data_[this.id]=val;
                       this.$set(this.data[this.dataField],this.index,data_);
                   }else{
                      // console.log("index==============="+this.index+"=="+this.id+"=="+val);
                       if(this.index!=""&&(this.index==0||this.index>0)){
                           //console.log("11111111111111111111");
                           //this.$set(this.data[this.index],this.id,val)
                           this.$set(this.data[this.pId][this.index],this.cId,val)
                       }else
                           this.$set(this.data[this.pId],this.cId,val)
                   }
               }else{
                  
                   this.$set(this.data[this.index],"vals",val);
               }
               if((this.extype=="date_year_start"||this.extype=="date_month_start")&&val){
                   let num=this.exkey.num;
                   let endDate=this.exkey.endDate;
                   let num_val=this.data[this.pId][num];
                   let num_val2="0";
                   if(this.exkey.month){
                       num_val2=this.data[this.pId][this.exkey.month];
                   }
                  
                   let date=new Date(val);
                   if(this.extype=="date_year_start")
                       date.setFullYear(date.getFullYear()+Number(num_val));
                   if(this.extype=="date_month_start")
                       date.setMonth(date.getMonth()+Number(num_val)); 
                  // alert(val+"="+date.getMonth()+"="+Number(val))
                   if(num_val2){
                       date.setMonth(date.getMonth()+Number(num_val2)); 
                   }
                   num_val2=num_val2=="" ? "0":num_val2;
                   if(Number(num_val)!=0||Number(num_val2)!=0){
                       if(this.exkey.nosubday!='Y')
                           date=date.setDate(date.getDate()-1);
                       date=new Date(date);
                   }
                   var year = date.getFullYear();//当前时间的年份减去一年
                   var month = date.getMonth()+1;//时间的月份获取
                   var day = date.getDate();//时间的日，获取
                  
                 
                
                   if(month<10){
                       month="0"+month;
                   }
                   if(day<10){
                       day="0"+day;
                   }
                   var time = year+'-'+month+'-'+day;//时间进行拼接
                  // alert(time)
                   this.$set(this.data[this.pId],this.exkey.endDate,time);
               }
               
              
           },
           value:function (val) { //对值进行验证0
               
           },
           dataFields:{
               handler: function (newVal) {
                   this.setData()
               },      
               deep: true    //深度监听
           }
       }
   }
</script>
<style scoped>
.formitem{
   margin-left:35px;
}
.formitem2{
   margin-left:10px;
}
.el-date-editor.el-input, .el-date-editor.el-input__inner{
   width:100% !important;
}
</style>
<style >
.el-date-table td.today span{
   
  font-size:18px;
}
.el-date-table td, .el-date-table td div{
   height:20px  !important;  
}
.el-picker-panel__content th{
   background:#409EFF;
   color:#fff !important;  
}
.el-picker-panel__content table{
   border:1px solid #409EFF;
}
.ivu-select-dropdown {
   z-index:99999 !important;
}
</style>